package com.scau.hwadee.examination.controller;


import com.scau.hwadee.examination.entity.Majororientation;
import com.scau.hwadee.examination.service.MajorOrientationService;
import com.scau.hwadee.examination.utils.JsonUtils;
import com.scau.hwadee.examination.utils.UniqueIDGeneratorUtils;
import com.scau.hwadee.examination.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: wrh
 * @Date: 2019/8/2 11:23
 */
@Controller
@RequestMapping("/MajorOrientation")
@Api(tags="MajorOrientationController", description="专业方向管理")
public class MajorOrientationController {
    @Autowired
    private MajorOrientationService majorOrientationService;

    @PostMapping (value = "/majorOrientation")
    @ApiOperation(value="添加专业方向")
    public ResponseEntity<ResponseVo> addMajorOrientation(
                 @RequestBody String params){
        Majororientation majororientation;
        majororientation=JsonUtils.JSONToObject(params,Majororientation.class,null);
        majororientation.setMrId(new UniqueIDGeneratorUtils.UUIDUniqueIDGeneratorUtils().
                                                          generatorId().substring(20));
        ResponseVo vo=ResponseVo.instance();
        int cnt=majorOrientationService.addMajororientation(majororientation);
        if(cnt>0)
        vo.setMessage("添加成功");
        else vo.setMessage("添加失败");
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @PutMapping(value = "/majorOrientation")
    @ApiOperation(value="修改专业方向")
    public ResponseEntity<ResponseVo> updateMajorOrientation(
            @RequestBody String params){
        Majororientation majororientation;
        majororientation=JsonUtils.JSONToObject(params,Majororientation.class,null);
        ResponseVo vo=ResponseVo.instance();
        int cnt=majorOrientationService.UpdateMajororientation(majororientation);
        if(cnt>0)
            vo.setMessage("修改成功");
        else vo.setMessage("修改失败");
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }

    @GetMapping (value = "/majorOrientationByMajor")
    @ApiOperation(value="根据专业编码查找专业方向")
    public ResponseEntity<ResponseVo> getMajorOrientationByMajor(
            @RequestParam("mjId") String mjId){
        ResponseVo vo=ResponseVo.instance();
        vo.setData(majorOrientationService.getMajororientationByMajor(mjId));
        return new ResponseEntity<>(vo, HttpStatus.OK);
    }
}
